package com.smartjobs.action;

import java.util.ArrayList;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.smartjobs.bean.CompanyBean;
import com.smartjobs.bean.JobpostingCompanySkillBean;
import com.smartjobs.bean.JobseekerBean;
import com.smartjobs.model.LoginModel;
import com.smartjobs.model.ViewCompanyDataModel;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {

	String logintype;
	String emailID;
	String password;
	JobseekerBean jobseeker;
	CompanyBean company;
	ArrayList<JobpostingCompanySkillBean> jobpostingList;

	public ArrayList<JobpostingCompanySkillBean> getJobpostingList() {
		return jobpostingList;
	}

	public void setJobpostingList(
			ArrayList<JobpostingCompanySkillBean> jobpostingList) {
		this.jobpostingList = jobpostingList;
	}

	public JobseekerBean getJobseeker() {
		return jobseeker;
	}

	public void setJobseeker(JobseekerBean jobseeker) {
		this.jobseeker = jobseeker;
	}

	public CompanyBean getCompany() {
		return company;
	}

	public void setCompany(CompanyBean company) {
		this.company = company;
	}

	public String getLogintype() {
		return logintype;
	}

	public void setLogintype(String logintype) {
		this.logintype = logintype;
	}

	public String getEmailID() {
		return emailID;
	}

	public void setEmailID(String emailID) {
		this.emailID = emailID;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() {

		return SUCCESS;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String login() {

		// System.out.println("Login Type: " + logintype + "; Email Address: "
		// + emailID + "; Password: " + password);

//		if (emailID.equals("")) {
//			addActionError("Please enter email address.");
//			return ERROR;
//		}
//
//		if (password.equals("")) {
//			addActionError("Please enter password.");
//			return ERROR;
//		}

		Map session = ActionContext.getContext().getSession();

		if (logintype.equalsIgnoreCase("jobseeker")) {

			jobseeker = new JobseekerBean();
			jobseeker.setEmailID(emailID);
			jobseeker.setPassword(password);

			if (LoginModel.findJobseeker(jobseeker)) {

				session.put("login", true);
				session.put("loginType", "jobseeker");
				session.put("userName", jobseeker.getName());
				session.put("jobseekerID", jobseeker.getJobSeekerID());
				System.out.println("UserName: " + session.get("userName"));
				System.out.println("jobseeker name: " + jobseeker.getName());
				return "JOBSEEKER";

			}

			else {
				addActionError("Incorrect Username/Password.");
				return ERROR;
			}

		}

		if (logintype.equalsIgnoreCase("company")) {

			company = new CompanyBean();
			company.setEmailID(emailID);
			company.setCompanyPassword(password);
			jobpostingList = new ArrayList<JobpostingCompanySkillBean>();

			if (LoginModel.findCompany(company)) {

				session.put("login", true);
				session.put("loginType", "company");
				session.put("userName", company.getCompanyName());
				session.put("companyID", company.getCompanyID());
				System.out.println("UserName: " + session.get("userName"));
				ViewCompanyDataModel.GetJobpostings(company.getCompanyID(), jobpostingList);
				return "COMPANY";

			}

			else {
				addActionError("Incorrect Username/Password.");
				return ERROR;
			}

		}

		if (logintype.equalsIgnoreCase("trainer")) {
			
			int trainerId=LoginModel.findTrainer(emailID);
			if(trainerId!=-1)
			 {
				session.put("login", true);
				session.put("loginType", "trainer");
				session.put("userName", emailID);
				session.put("trainerId", trainerId);
				System.out.println("returning trainer");
				return "TRAINER";
			} else {
				addActionError("Incorrect Username/Password.");
				return ERROR;
			}
			
			

		}

		return ERROR;

	}

	@SuppressWarnings("rawtypes")
	public String logout() {
		Map session = ActionContext.getContext().getSession();
		session.clear();
		return SUCCESS;
	}

}
